package com.prawn.trace.record.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.prawn.trace.record.dao.RecordSaleDao;
import com.prawn.trace.record.pojo.RecordOutofware;
import com.prawn.trace.record.pojo.RecordSale;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;
import util.NullAttributeUtils;


/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class RecordSaleService {

	@Autowired
	private RecordSaleDao recordSaleDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<RecordSale> findAll() {
		Sort sort=new Sort(Sort.Direction.DESC,"createDate");
		return recordSaleDao.findAll(sort);
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<RecordSale> findSearch(Map whereMap, int page, int size) {
		Specification<RecordSale> specification = createSpecification(whereMap);
		Sort sort=new Sort(Sort.Direction.DESC,"createDate");
		PageRequest pageRequest =  PageRequest.of(page-1, size,sort);
		return recordSaleDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<RecordSale> findSearch(Map whereMap) {
		Sort sort=new Sort(Sort.Direction.DESC,"createDate");
		Specification<RecordSale> specification = createSpecification(whereMap);
		return recordSaleDao.findAll(specification,sort);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public RecordSale findById(String id) {
		return recordSaleDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param recordSale
	 */
	public void add(RecordSale recordSale) {
		recordSale.setId( idWorker.nextId()+"" );
		recordSaleDao.save(recordSale);
	}

	/**
	 * 修改
	 * @param recordSale
	 */
	public void update(RecordSale recordSale) {
		RecordSale oldRecordSale =recordSaleDao.findById(recordSale.getId()).get();
		BeanUtils.copyProperties(recordSale,oldRecordSale, NullAttributeUtils.getNullPropertyNames(recordSale));
		recordSaleDao.save(oldRecordSale);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		recordSaleDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<RecordSale> createSpecification(Map searchMap) {

		return new Specification<RecordSale>() {

			@Override
			public Predicate toPredicate(Root<RecordSale> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // id
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 商品名称
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 销售的目标产产品批次id
                if (searchMap.get("produceBatchId")!=null && !"".equals(searchMap.get("produceBatchId"))) {
                	predicateList.add(cb.like(root.get("produceBatchId").as(String.class), "%"+(String)searchMap.get("produceBatchId")+"%"));
                }
                // 销售人
                if (searchMap.get("salePerson")!=null && !"".equals(searchMap.get("salePerson"))) {
                	predicateList.add(cb.like(root.get("salePerson").as(String.class), "%"+(String)searchMap.get("salePerson")+"%"));
                }
                // 销售商家（可引用场地中的信息）
                if (searchMap.get("saleCompany")!=null && !"".equals(searchMap.get("saleCompany"))) {
                	predicateList.add(cb.like(root.get("saleCompany").as(String.class), "%"+(String)searchMap.get("saleCompany")+"%"));
                }
                // 所属的生产流程id
                if (searchMap.get("processId")!=null && !"".equals(searchMap.get("processId"))) {
                	predicateList.add(cb.like(root.get("processId").as(String.class), "%"+(String)searchMap.get("processId")+"%"));
                }
                // 销售方式
                if (searchMap.get("saleWay")!=null && !"".equals(searchMap.get("saleWay"))) {
                	predicateList.add(cb.like(root.get("saleWay").as(String.class), "%"+(String)searchMap.get("saleWay")+"%"));
                }
                // 备注信息
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                	predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
                // 更新者
                if (searchMap.get("updateBy")!=null && !"".equals(searchMap.get("updateBy"))) {
                	predicateList.add(cb.like(root.get("updateBy").as(String.class), "%"+(String)searchMap.get("updateBy")+"%"));
                }
                // 创建者
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.equal(root.get("createBy").as(String.class), (String)searchMap.get("createBy")));
                }
                // 删除标记（虚拟删除，并不删除数据库表数据，0：正常，1：删除）
                if (searchMap.get("delFlag")!=null && !"".equals(searchMap.get("delFlag"))) {
                	predicateList.add(cb.like(root.get("delFlag").as(String.class), "%"+(String)searchMap.get("delFlag")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
